package com.dds.system.service.union.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dds.system.domain.union.UnionShopSubsidy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dds.system.mapper.union.UnionShopSubsidyPeriodMapper;
import com.dds.system.domain.union.UnionShopSubsidyPeriod;
import com.dds.system.service.union.IUnionShopSubsidyPeriodService;

/**
 * 商家特殊补贴 不同时段的比例定义Service业务层处理
 * 
 * @author gaolianjie
 * @date 2021-04-16
 */
@Service
public class UnionShopSubsidyPeriodServiceImpl implements IUnionShopSubsidyPeriodService 
{
    @Autowired
    private UnionShopSubsidyPeriodMapper unionShopSubsidyPeriodMapper;

    /**
     * 查询商家特殊补贴 不同时段的比例定义
     * 
     * @param id 商家特殊补贴 不同时段的比例定义ID
     * @return 商家特殊补贴 不同时段的比例定义
     */
    @Override
    public UnionShopSubsidyPeriod selectUnionShopSubsidyPeriodById(Integer id)
    {
        return unionShopSubsidyPeriodMapper.selectUnionShopSubsidyPeriodById(id);
    }

    /**
     * 查询商家特殊补贴 不同时段的比例定义列表
     * 
     * @param shopId 商家id
     * @return 商家特殊补贴 不同时段的比例定义
     */
    @Override
    public List<Map<String, Object>> selectUnionShopSubsidyPeriodList(String shopId)
    {
        List<UnionShopSubsidyPeriod> lst = unionShopSubsidyPeriodMapper.selectUnionShopSubsidyPeriodList(shopId);

        List<Map<String, Object>> returnData = new ArrayList<>();

        for (UnionShopSubsidyPeriod info : lst) {
            Map<String, Object> data = new HashMap<>();

            Long periodStart = info.getPeriodStart();
            Long periodEnd = info.getPeriodEnd();

            data.put("periodStart", (int) (periodStart / 60) + ":" + (periodStart % 60));
            data.put("periodEnd", (int) (periodEnd / 60) + ":" + (periodEnd % 60));

            data.put("subsidyRatioMax", info.getSubsidyRatioMax().multiply(new BigDecimal("100")));
            data.put("subsidyRatioMin", info.getSubsidyRatioMin().multiply(new BigDecimal("100")));
            returnData.add(data);
        }

        return returnData;
    }

    /**
     * 新增商家特殊补贴 不同时段的比例定义
     * 
     * @param unionShopSubsidyPeriod 商家特殊补贴 不同时段的比例定义
     * @return 结果
     */
    @Override
    public int insertUnionShopSubsidyPeriod(List<Map<String, Object>> params, String shopId)
    {
        if (params.size() > 0) {
            for (Map<String, Object> map : params) {
                Long[] ptime = new Long[2];

                List<String> periodTimes = (List<String>) map.get("periodTime");

                for (int i = 0; i < periodTimes.size(); i++) {
                    String[] split = periodTimes.get(i).split(":");

                    ptime[i] = Integer.parseInt(split[0]) * 60L + Integer.parseInt(split[1]);
                }

                UnionShopSubsidyPeriod period = UnionShopSubsidyPeriod.of()
                        .setShopId(shopId)
                        .setSubsidyRatioMin(new BigDecimal(map.get("subsidyRatioMin").toString()).divide(BigDecimal.valueOf(100)))
                        .setSubsidyRatioMax(new BigDecimal(map.get("subsidyRatioMax").toString()).divide(BigDecimal.valueOf(100)))
                        .setPeriodStart(ptime[0])
                        .setPeriodEnd(ptime[1]);

                unionShopSubsidyPeriodMapper.insertUnionShopSubsidyPeriod(period);
            }
        }

        return 1;
    }

    /**
     * 批量删除商家特殊补贴 不同时段的比例定义
     * 
     * @param ids 需要删除的商家特殊补贴 不同时段的比例定义ID
     * @return 结果
     */
    @Override
    public int deleteUnionShopSubsidyPeriodByIds(Integer[] ids)
    {
        return unionShopSubsidyPeriodMapper.deleteUnionShopSubsidyPeriodByIds(ids);
    }

    /**
     * 删除商家特殊补贴 不同时段的比例定义信息
     * 
     * @param id 商家特殊补贴 不同时段的比例定义ID
     * @return 结果
     */
    @Override
    public int deleteUnionShopSubsidyPeriodById(Integer id)
    {
        return unionShopSubsidyPeriodMapper.deleteUnionShopSubsidyPeriodById(id);
    }
}
